//
//  NewsViewController.m
//  News
//
//  Created by wangshan on 16/2/25.
//  Copyright © 2016年 News. All rights reserved.
//


#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height

#import "NewsViewController.h"
#import "News_toutiaoViewController.h"
#import "News_autoViewController.h"
#import "News_entViewController.h"
#import "News_sportsViewController.h"
#import "News_techViewController.h"
#import "News_financeViewController.h"
#import "News_funnyViewController.h"

#import <AFNetworking.h>

@interface NewsViewController ()<UIScrollViewDelegate>

@property (nonatomic,strong) UIScrollView *containScroll;
@property (nonatomic,strong) UIScrollView *titleScroll;
@property (nonatomic,strong) NSMutableArray *titleButtons;
@property (nonatomic,strong) UIButton *selectButton;

@property (nonatomic,strong) NSMutableArray *categoryModelArray;


@end

@implementation NewsViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupContainScrollView];
    [self setupChirdController];
    [self setupTitleScroll];
    self.containScroll.contentSize = CGSizeMake(self.childViewControllers.count *kScreenW, 0);
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

#pragma mark - 视图框架

// 标题button懒加载
- (NSMutableArray *)titleButtons {
    if (!_titleButtons) {
        _titleButtons = [NSMutableArray array];
    }
    return _titleButtons;
}

//添加滚动视图
- (void)setupContainScrollView {
    
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    scroll.backgroundColor = [UIColor whiteColor];
    scroll.pagingEnabled = YES;
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.delegate = self;
    
    [self.view addSubview:scroll];
    self.containScroll = scroll;
}

//添加子控制器
- (void)setupChirdController {
    
    //头条
    News_toutiaoViewController *toutiaoVC = [[News_toutiaoViewController alloc] init];
    toutiaoVC.title = @"头条";
    [self addChildViewController:toutiaoVC];
    
    //娱乐
    News_entViewController *entVC = [[News_entViewController alloc] init];
    entVC.title = @"娱乐";
    [self addChildViewController:entVC];
    
    //体育
    News_sportsViewController *sportsVC = [[News_sportsViewController alloc] init];
    sportsVC.title = @"体育";
    [self addChildViewController:sportsVC];
    
    //财经
    News_financeViewController *financeVC = [[News_financeViewController alloc] init];
    financeVC.title = @"财经";
    [self addChildViewController:financeVC];
    
    //科技
    News_techViewController *techVC = [[News_techViewController alloc] init];
    techVC.title = @"科技";
    [self addChildViewController:techVC];
    
    //汽车
    News_autoViewController *autoVC = [[News_autoViewController alloc] init];
    autoVC.title = @"汽车";
    [self addChildViewController:autoVC];
    
    //搞笑
    News_funnyViewController *funnyVC = [[News_funnyViewController alloc] init];
    funnyVC.title = @"搞笑";
    [self addChildViewController:funnyVC];
}

// 添加滚动标题
- (void)setupTitleScroll {
    
    UIScrollView *view = [[UIScrollView alloc] init];
    view.frame = CGRectMake(0, 0, kScreenW, 44);
    view.showsHorizontalScrollIndicator = NO;
    self.titleScroll = view;
    
    CGFloat buttonW = 70;
    CGFloat buttonH = view.frame.size.height;
    for (int i = 0; i < self.childViewControllers.count; i++) {
        
        UIViewController *vc = self.childViewControllers[i];
        
        UIButton *button = [[UIButton alloc] init];
        button.frame = CGRectMake(buttonW * i, 0, buttonW, buttonH);
        [button setTitle:vc.title forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(TopClickButton:) forControlEvents:(UIControlEventTouchUpInside)];
        button.tag = i;
        [view addSubview:button];
        
        if (button.tag == 0) {
            [self TopClickButton:button];
        }
        
        [self.titleButtons addObject:button];
        
    }
    self.navigationItem.titleView = view;
    self.titleScroll.contentSize = CGSizeMake(buttonW * self.childViewControllers.count, buttonH);
}

// 设置选中button
- (void)TopClickButton:(UIButton *)button {
    
    [self selectedButton:button];
    
    [self selectChirdController:button];
}

// 点击按钮方法
- (void)selectedButton:(UIButton *)button {
    
    [_selectButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    
    [button setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    
    [self setupTitleButtonCenter:button];
    
    _selectButton = button;
}

// 设置点击按钮后,按钮居中
- (void)setupTitleButtonCenter:(UIButton *)button {
    //修改偏移量
    CGFloat offsetX = button.center.x - kScreenW*0.5;
    //处理最小滚动偏移量
    if (offsetX < 0) {
        offsetX = 0;
    }
    
    //处理最大滚动偏移量
    CGFloat maxOffsetX = self.titleScroll.contentSize.width - kScreenW;
    if (offsetX > maxOffsetX) {
        offsetX = maxOffsetX;
    }
    [self.titleScroll setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

// 设置选中的子控制器
- (void)selectChirdController:(UIButton *)button {
    
    UIViewController *vc = self.childViewControllers[button.tag];

    vc.view.frame = CGRectMake(button.tag * kScreenW, 64, kScreenW, kScreenH - 64);
    
    [self.containScroll addSubview:vc.view];
    
    self.containScroll.contentOffset = CGPointMake(button.tag * kScreenW, 0);
}

// 滚动方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger i = scrollView.contentOffset.x / kScreenW;
    
    UIButton *button = self.titleButtons[i];
    
    [self selectedButton:button];
    
    [self selectChirdController:button];
    
    
}

#pragma mark - 数据加载
// categoryModelArray 懒加载
- (NSMutableArray *)categoryModelArray {
    if (!_categoryModelArray) {
        _categoryModelArray = [NSMutableArray array];
    }
    return _categoryModelArray;
}

//-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
//    return UIInterfaceOrientationMaskPortrait;
//}


- (BOOL)shouldAutorotate {
    return YES;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    
    return UIInterfaceOrientationPortrait;
}

@end
